package system

import (
	"fresh-shop-gin/middleware"
	"fresh-shop-gin/model/common"
	"fresh-shop-gin/model/system"
	"fresh-shop-gin/model/system/request"
	"fresh-shop-gin/model/system/response"
	"fresh-shop-gin/utils"
	"net/http"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type UserController struct{}

// Login 登录
// @Summary 用户登录
// @Tags 用户
// @Accept json
// @Produce json
// @Param user body system.LoginRequest true "用户登录信息"
// @Success 200 {object} system.LoginResponse
// @Router /api/login [post]
func (u *UserController) Login(c *gin.Context) {
	var loginModel request.Login
	if err := c.ShouldBindJSON(&loginModel); err != nil {
		utils.Logger.Error("参数错误", zap.Error(err))
		common.Error(c, http.StatusBadRequest, common.CodeBadRequest, "参数错误")
		return
	}

	loginUser := &system.User{UserName: loginModel.UserName, Password: loginModel.Password}
	user, err := userService.Login(loginUser)
	if err != nil {
		utils.Logger.Error("登录失败", zap.Error(err))
		common.Error(c, http.StatusInternalServerError, common.CodeInternalError, "登录失败")
		return
	}

	// 引入jwt
	token, err := utils.GenerateToken(user.ID, user.UserName)
	if err != nil {
		utils.Logger.Error("生成token失败", zap.Error(err))
		common.Error(c, http.StatusInternalServerError, common.CodeInternalError, "生成token失败")
		return
	}

	common.Success(c, &response.LoginResponse{Token: token})
}

// Register 注册
// @Summary 用户注册
// @Tags 用户
// @Accept json
// @Produce json
// @Param user body system.RegisterRequest true "用户注册信息"
// @Success 200 {object} system.RegisterResponse
// @Router /api/register [post]
func (u *UserController) Register(c *gin.Context) {
	var registerModel request.Register
	if err := c.ShouldBindJSON(&registerModel); err != nil {
		utils.Logger.Error("参数错误", zap.Error(err))
		common.Error(c, http.StatusBadRequest, common.CodeBadRequest, "参数错误")
		return
	}

	node, err := utils.NewWorker(1)
	if err != nil {
		utils.Logger.Error("生成用户ID失败", zap.Error(err))
		common.Error(c, http.StatusInternalServerError, common.CodeInternalError, "生成用户ID失败")
		return
	}
	user := &system.User{
		ID:       node.GetId(),
		UserName: registerModel.UserName,
		Password: registerModel.Password,
	}

	if err := userService.Register(user); err != nil {
		utils.Logger.Error("注册用户失败", zap.Error(err))
		common.Error(c, http.StatusInternalServerError, common.CodeInternalError, "注册失败")
		return
	}

	common.Success(c, nil)
}

// GetUserList 获取用户列表
// @Summary 获取用户列表
// @Tags 用户
// @Accept json
// @Produce json
// @Success 200 {object} system.User
// @Router /api/user/list [get]
func (u *UserController) GetUserList(c *gin.Context) {
	claims := middleware.GetUserInfo(c)
	if claims == nil {
		utils.Logger.Error("未登录")
		common.Error(c, http.StatusUnauthorized, common.CodeUnauthorized, "未登录")
		return
	}

	users, err := userService.GetUserList()
	if err != nil {
		utils.Logger.Error("获取用户列表失败", zap.Error(err))
		common.Error(c, http.StatusInternalServerError, common.CodeInternalError, "获取用户列表失败")
		return
	}

	common.Success(c, users)
}
